# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>

_realname=libimagequant
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.0.4
pkgrel=3
pkgdesc="Palette quantization library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://github.com/ImageOptim/libimagequant'
license=("GPL3" "MIT")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cargo-c")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}2")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/ImageOptim/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('d121bbfb380a54aca8ea9d973c2e63afcbc1db67db46ea6bc63eeba44d7937c8')

prepare() {
  cp -r ${_realname}-${pkgver} build-${MSYSTEM}
  ${MINGW_PREFIX}/bin/cargo fetch \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

build() {
  ${MINGW_PREFIX}/bin/cargo cbuild \
    --release \
    --frozen \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml
}

package() {
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/cargo cinstall \
    --frozen \
    --offline \
    --destdir="${pkgdir}" \
    --prefix=${MINGW_PREFIX} \
    --manifest-path build-${MSYSTEM}/imagequant-sys/Cargo.toml

  # Remove def file
  rm -f ${pkgdir}${MINGW_PREFIX}/lib/*.def

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/doc/libimagequant
  install -m 0644 -p ${_realname}-${pkgver}/CHANGELOG \
    "${pkgdir}"${MINGW_PREFIX}/share/doc/libimagequant/CHANGELOG
  install -m 0644 -p ${_realname}-${pkgver}/README.md \
    "${pkgdir}"${MINGW_PREFIX}/share/doc/libimagequant/README.md

  mkdir -p "${pkgdir}"${MINGW_PREFIX}/share/licenses/libimagequant
  install -m 0644 -p ${_realname}-${pkgver}/COPYRIGHT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/libimagequant/COPYRIGHT
}

